package com.heima.user.test;

import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Program: day57-leadnews
 * @Description: 用友AI测试
 * @Data: 2022/7/28 21:28
 **/
public class YongYouAI {
    //基于restTemplate调用接口
//    @Test
//    public void idcardTest(){
//        // Spring提供的http客户端
//        RestTemplate restTemplate = new RestTemplate();
//        // 设置请求头
//        HttpHeaders httpHeaders = new HttpHeaders();
//        // 设置用户中的apicode
//        httpHeaders.add("apicode","27461bfd82fd4d549a81753c5c259fa5");
//        // 设置请求参数为 json 类型
//        httpHeaders.setContentType(MediaType.APPLICATION_JSON); // json
//        // 根据接口文档封装参数
//        Map<String,String> params = new HashMap<>();
//        params.put("idNumber","210103195103222113");
//        params.put("userName","王东镇");
//        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(params), httpHeaders); // 封装请求参数
//        ResponseEntity<String> response = restTemplate.postForEntity("https://api.yonyoucloud.com/apis/dst/matchIdentity/matchIdentity", formEntry, String.class);// 发送一个post请求
//        // 响应体内容
//        String body = response.getBody();
//        Map map = JSON.parseObject(body, Map.class);
//        System.out.println(map);
////        System.out.println(response);
//    }

    @Test
    public void idcardTest(){
        // 根据接口文档封装参数
        Map<String,String> params = new HashMap<>();
        params.put("idNumber","210103195103222113");
        params.put("userName","王东镇");
        Map map = executePostRequest("27461bfd82fd4d549a81753c5c259fa5", params, "https://api.yonyoucloud.com/apis/dst/matchIdentity/matchIdentity");
        System.out.println(map);
    }

    public Map executePostRequest(String apiCode,Map<String,String> params,String url){
        // Spring提供的http客户端
        RestTemplate restTemplate = new RestTemplate();
        // 设置请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        // 设置用户中的apicode
        httpHeaders.add("apicode",apiCode);
        // 设置请求参数为 json 类型
        httpHeaders.setContentType(MediaType.APPLICATION_JSON); // json
        // 根据接口文档封装参数

        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(params), httpHeaders); // 封装请求参数
        ResponseEntity<String> response = restTemplate.postForEntity(url, formEntry, String.class);// 发送一个post请求
        // 响应体内容
        String body = response.getBody();
        Map map = JSON.parseObject(body, Map.class);
//        System.out.println(map);
        return map;

//        System.out.println(response);
    }


    @Test
    public void orcTest(){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("apicode","16dcb92cc5c2403d9142b01445c61b21");
        httpHeaders.setContentType(MediaType.APPLICATION_JSON); // json
        Map<String,String> map = new HashMap<>();
        map.put("image","https://hmtt122.oss-cn-shanghai.aliyuncs.com/demo_idcard.png");
        map.put("imageType","URL");
        map.put("ocrType","0");
        HttpEntity<String> formEntry = new HttpEntity<>(JSON.toJSONString(map), httpHeaders); // 封装请求参数
        ResponseEntity<String> response = restTemplate.postForEntity("https://api.yonyoucloud.com/apis/dst/IdcardOCR/IdcardOCR", formEntry, String.class);// 发送一个post请求
        System.out.println(response);
    }

}
